package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.system.domain.Zyrecharge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ZyrechargeMapper;
import com.ruoyi.system.service.IZyrechargeService;

/**
 * 住院充值记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-30
 */
@Service
public class ZyrechargeServiceImpl implements IZyrechargeService 
{
    @Autowired
    private ZyrechargeMapper zyrechargeMapper;

    /**
     * 查询住院充值记录
     * 
     * @param id 住院充值记录主键
     * @return 住院充值记录
     */
    @Override
    public Zyrecharge selectZyrechargeById(Long id)
    {
        return zyrechargeMapper.selectZyrechargeById(id);
    }

    /**
     * 查询住院充值记录列表
     * 
     * @param zyrecharge 住院充值记录
     * @return 住院充值记录
     */
    @Override
    public List<Zyrecharge> selectZyrechargeList(Zyrecharge zyrecharge)
    {
         // 查询最新余额
        BigDecimal lastBalance = zyrechargeMapper.selectLatestBalance(zyrecharge.getId());
        if (lastBalance == null) lastBalance = BigDecimal.ZERO;

        // 计算新余额
        if ("已支付".equals(zyrecharge.getPayStatus())) {
            zyrecharge.setBalance(lastBalance.add(zyrecharge.getAmount()));
        } else {
            zyrecharge.setBalance(lastBalance);
        }
        return zyrechargeMapper.selectZyrechargeList(zyrecharge);
    }

    /**
     * 新增住院充值记录
     * 
     * @param zyrecharge 住院充值记录
     * @return 结果
     */
    @Override
    public int insertZyrecharge(Zyrecharge zyrecharge)
    {
         // 查询最新余额
        BigDecimal lastBalance = zyrechargeMapper.selectLatestBalance(zyrecharge.getId());
        if (lastBalance == null) lastBalance = BigDecimal.ZERO;

        // 计算新余额
        if ("已支付".equals(zyrecharge.getPayStatus())) {
            zyrecharge.setBalance(lastBalance.add(zyrecharge.getAmount()));
        } else {
            zyrecharge.setBalance(lastBalance);
        }
        zyrecharge.setOperateTime(new Date());
        return zyrechargeMapper.insertZyrecharge(zyrecharge);
    }

    /**
     * 修改住院充值记录
     * 
     * @param zyrecharge 住院充值记录
     * @return 结果
     */
    @Override
    public int updateZyrecharge(Zyrecharge zyrecharge)
    {
        return zyrechargeMapper.updateZyrecharge(zyrecharge);
    }

    /**
     * 批量删除住院充值记录
     * 
     * @param ids 需要删除的住院充值记录主键
     * @return 结果
     */
    @Override
    public int deleteZyrechargeByIds(Long[] ids)
    {
        return zyrechargeMapper.deleteZyrechargeByIds(ids);
    }

    /**
     * 删除住院充值记录信息
     * 
     * @param id 住院充值记录主键
     * @return 结果
     */
    @Override
    public int deleteZyrechargeById(Long id)
    {
        return zyrechargeMapper.deleteZyrechargeById(id);
    }
}
